﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.PeiXunService;

namespace CommunitySystem
{
    public partial class AddPeiXunEmpWindow : ChildWindow
    {
        public string OrgIDs = "";
        public int PeiXunID;
        public int PiaoShu;
        private JinShengService.JinShengServiceClient _Client = new JinShengService.JinShengServiceClient();
        public ObservableCollection<JinShengService.Emp_BasicInfoModel> SelectItems = new ObservableCollection<JinShengService.Emp_BasicInfoModel>();
        private PeiXunService.PeiXunServiceClient client1 = new PeiXunService.PeiXunServiceClient();
        public AddPeiXunEmpWindow(string orgids,int peixunid,int piaoshu)
        {
            InitializeComponent();
            OrgIDs = orgids;
            PeiXunID = peixunid;
            PiaoShu = piaoshu;
            _Client.GetEmpBasicInfoAccordingToDutyCompleted +=
                new EventHandler<JinShengService.GetEmpBasicInfoAccordingToDutyCompletedEventArgs>(_Client_GetEmpBasicInfoAccordingToDutyCompleted);
            tbCondition.AddHandler(TextBox.MouseLeftButtonDownEvent, new MouseButtonEventHandler(TextBox_MouseLeftButtonDown), true);
            client1.AddPeiXunEmpDetailModelCompleted += new EventHandler<AddPeiXunEmpDetailModelCompletedEventArgs>(client1_AddPeiXunEmpDetailModelCompleted);
        }

        void client1_AddPeiXunEmpDetailModelCompleted(object sender, AddPeiXunEmpDetailModelCompletedEventArgs e)
        {
            if(e.Result == true)
            {
                this.DialogResult = true;
            }
        }

        private void ChildWindow_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                btnAdminSearch_Click(null, null);
            }
        }

        private void TextBox_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (!string.IsNullOrEmpty(tbCondition.Text) && tbCondition.Text.Equals("请输入姓名或证件号码"))
            {
                tbCondition.SelectAll();
            }
        }

        private void tbCondition_KeyDown(object sender, KeyEventArgs e)
        {
            if (!string.IsNullOrEmpty(tbCondition.Text) && tbCondition.Text.Equals("请输入姓名或证件号码"))
            {
                tbCondition.FontStyle = FontStyles.Normal;
                //tbCondition.Text = e.Key.ToString();
                tbCondition.Foreground = new SolidColorBrush(Colors.Black);
            }
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            ObservableCollection<PeiXunEmpDetailModel> results = new ObservableCollection<PeiXunEmpDetailModel>();
            int qita1 = 0;
            SelectItems = null;
            var source = lb.ItemsSource as System.Collections.ObjectModel.ObservableCollection<CommunitySystem.JinShengService.Emp_BasicInfoModel>;
            SelectItems = new ObservableCollection<JinShengService.Emp_BasicInfoModel>(source.Where(c => c.IsSelected == true));
            if (SelectItems.Count > 0)
            {
                if(!string.IsNullOrEmpty(qitazhang.Text.Trim())) qita1 = Convert.ToInt32(qitazhang.Text.Trim());
                if (SelectItems.Count + qita1 > PiaoShu)
                {
                    SilverlightMessageBoxLibrary.Message.ErrorMessage("所选人数多于预定票数！");
                    return;
                }
                if (MessageBox.Show("确定选择所选人员吗？", "系统提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    foreach(CommunitySystem.JinShengService.Emp_BasicInfoModel i in SelectItems)
                    {
                        PeiXunEmpDetailModel model = new PeiXunEmpDetailModel
                                                         {EmpID = i.ID, PeiXunID = PeiXunID, Memo = qita.Text.Trim(),OrgID = Convert.ToInt32(i.OrgID)};
                        results.Add(model);
                    }
                    if (qita1 != 0)
                    {
                        PeiXunEmpDetailModel model = new PeiXunEmpDetailModel { EmpID = 0, PeiXunID = PeiXunID, Memo = qita.Text.Trim(), QiTaZhang = qita1, OrgID = Convert.ToInt32(SelectItems[0].OrgID) };
                        results.Add(model);
                    }
                    client1.AddPeiXunEmpDetailModelAsync(results);
                    
                }
            }
            else SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择人员！");

        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
        private void btnAdminSearch_Click(object sender, RoutedEventArgs e)
        {
            string nameOrCard = "1=1";
            if (!string.IsNullOrEmpty(tbCondition.Text) && !tbCondition.Text.Equals("请输入姓名或证件号码"))
            {
                nameOrCard = tbCondition.Text;
                _Client.GetEmpBasicInfoAccordingToDutyAsync(string.Format(@" OrgID in ({0}) and KeChuJi = 0 and (Name like '%{1}%' or CardNo like '%{1}%') 
                ", OrgIDs, nameOrCard));
                // _empClient.GetEmp_BasicInfoAsync(_OrgID + "", "0", "", "", 0, 20, "在职", string.Format("emp.Name like '%{0}%' or emp.CardNo like '%{0}%'", nameOrCard));
            }
            else
            {
                _Client.GetEmpBasicInfoAccordingToDutyAsync(string.Format(" OrgID in ({0}) and KeChuJi = 0", OrgIDs));
            }
        }

        void _Client_GetEmpBasicInfoAccordingToDutyCompleted(object sender, JinShengService.GetEmpBasicInfoAccordingToDutyCompletedEventArgs e)
        {
            lb.ItemsSource = null;
            lb.ItemsSource = e.Result;
        }
    }
}
